package conson.tech.manage.facade.controller.activity.mobile;

import cn.hutool.json.JSONUtil;
import conson.tech.core.constant.SecurityConstants;
import conson.tech.core.response.Response;
import conson.tech.manage.activity.service.UserCreditRatingService;
import conson.tech.manage.api.activity.dto.UserCreditRatingDTO;
import conson.tech.manage.api.activity.form.UserCreditForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * ClassName: UserCreditController
 * Description: 用户信用控制器
 * Author: HXL
 * Date: 2020/5/26 5:04 下午
 * Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping(SecurityConstants.INNERAPI)
public class UserCreditController {

    @Autowired
    private UserCreditRatingService userCreditRatingService;

    @PostMapping("/userCredit")
    public Response<UserCreditRatingDTO> queryUserCredit(@Valid @RequestBody UserCreditForm form){
        log.info("UserCreditController->> queryUserCredit params:【{}】", JSONUtil.toJsonStr(form));
       UserCreditRatingDTO dto = userCreditRatingService.queryUserCredit(form.getUserMobile());
        return Response.success(dto);
    }
    @PostMapping("/activationUserCredit")
    public Response<UserCreditRatingDTO> activationUserCredit(@Valid @RequestBody UserCreditForm form){
        log.info("UserCreditController->> activationUserCredit params:【{}】", JSONUtil.toJsonStr(form));
        UserCreditRatingDTO dto = userCreditRatingService.queryUserCreditWhiteList(form.getUserMobile());
        UserCreditRatingDTO ratingDTO = null;
            if (dto == null){ //如果不是白名单那么就激活新增
                ratingDTO = userCreditRatingService.activationUserCredit(form.getUserMobile(),form.getUserId());
            }else {
                ratingDTO = userCreditRatingService.updateUserCredit(form.getUserMobile(),form.getUserId());
            }
        return Response.success(ratingDTO);
    }
}
